<template>
  <div>
    <div style="margin: 15px 0">
      <el-row :gutter="20">
        <#if vueSearchColList??>
          <#list vueSearchColList as col>
            <el-col :span="6">
              <el-input
                      clearable
                      placeholder="请输入${col["propertyChinaName"]}"
                      v-model="params.${col["propertyName"]}"
                      class="input-with-select">
              </el-input>
            </el-col>
          </#list>
        </#if>
        <#if vueSearchColList??>
        <el-col :span="2">
          <el-button icon="el-icon-search" @click="getTableData(1)">搜索</el-button>
        </el-col>
        <el-col :span="2">
          <el-button type="success" icon="el-icon-remove-outline" @click="resetObj">清空</el-button>
        </el-col>
        </#if>
        <el-col :span="2">
          <el-button type="primary" icon="el-icon-edit" @click="add">添加</el-button>
        </el-col>
        <el-col :span="2">
          <el-popover
                  placement="top"
                  width="160"
                  v-model="delVisible">
            <p>确定删除吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="delVisible = false">取消</el-button>
              <el-button type="primary" size="mini" @click="deleteByIds">确定</el-button>
            </div>
            <el-button slot="reference" type="danger" icon="el-icon-delete">批量删除</el-button>
          </el-popover>
        </el-col>
      </el-row>
    </div>
    <el-table :data="tableData" style="width: 100%" class="loading" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"></el-table-column>
      <#if vueShowColList??>
      <#list vueShowColList as col>
      <#--   如果是图片     -->
      <#if col["isUploadCol"]>
        <el-table-column prop="${col["propertyName"]}" label="${col["propertyChinaName"]}">
          <template slot-scope="scope">
            <img
                    :src="scope.row.${col["propertyName"]}"
                    class="baseImg"
                    @click="seeBigImg(scope.row.${col["propertyName"]})"
            />
            <el-upload
                    :action="getUploadUrl(scope.row.id,moduleName)"
                    :show-file-list="false"
                    :headers="header"
                    :on-success="handleAvatorSuccess"
                    :before-upload="beforeAvatorUpload"
            >
              <el-button size="mini">图片上传</el-button>
            </el-upload>
          </template>
        </el-table-column>
      <#elseif col["showType"] == 'text'>
        <el-table-column label="${col["propertyChinaName"]}" align="center" min-width="150px" prop="${col["propertyName"]}"></el-table-column>
      <#elseif col["showType"] == 'enum'>
        <el-table-column label="${col["propertyChinaName"]}" align="center" width="110px">
          <template slot-scope="{row}">
            <#if updateEnumsLists??>
              <#list tableEnumsEntities as enumCol>
                <#if col["propertyName"] == enumCol["colName"]>
                  <#if enumCol["enumsDTOS"]??>
                    <#list enumCol["enumsDTOS"] as enumCol1>
                      <strong v-if="row.${col["propertyName"]} == ${enumCol1["name"]}">${enumCol1["value"]}</strong>
                    </#list>
                  </#if>
                </#if>
              </#list>
            </#if>
          </template>
        </el-table-column>
      </#if>
      </#list>
      </#if>
      <el-table-column fixed="right" label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-link icon="el-icon-edit" type="primary" @click="updateById(scope.row,formData)">编辑</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-link icon="el-icon-delete" type="danger" @click="deleteById(scope.row.id)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
            style="margin: 10px auto"
            @size-change="pageSizeChange"
            @current-change="currentChange"
            :current-page="params.current"
            :page-sizes="[5, 10, 15, 100]"
            :page-size="params.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="params.total">
    </el-pagination>
    <el-dialog v-bind="$attrs" :show-close="false" :visible="openDia" v-on="$listeners" :title="diaTitle">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="120px">
        <#if vueModifyColList??>
          <#list vueModifyColList as col>
            <el-form-item label="${col["propertyChinaName"]}" prop="${col["propertyName"]}">
              <#if col["updateType"] == 'text'>
                <el-input v-model="formData.${col["propertyName"]}"/>
              <#elseif col["updateType"] == 'switch'>
                <el-switch
                        v-model="formData.${col["propertyName"]}"
                        active-text="开关状态一"
                        inactive-text="开关状态二">
                </el-switch>
              <#elseif col["updateType"] == 'select'>
                <el-select v-model="formData.${col["propertyName"]}" placeholder="请初始化下拉选择框数据">
                  <#if selectEntities??>
                    <#list selectEntities as selectCol>
                      <#if selectCol["propertyName"] == col["propertyName"]>
                        <el-option
                                v-for="item in  ${selectCol["className"]}Data"
                                :key="item.key"
                                :label="item.value"
                                :value="item.key">
                        </el-option>
                      </#if>
                    </#list>
                  </#if>
                </el-select>
              <#elseif col["updateType"] == 'enum'>
                <#if updateEnumsLists??>
                  <el-radio-group v-model="formData.${col["propertyName"]}">
                    <#list updateEnumsLists as enumCol>
                      <#if col["propertyName"] == enumCol["colName"]>
                        <#if enumCol["enumsDTOS"]??>
                          <#list enumCol["enumsDTOS"] as enumCol1>
                            <el-radio :label="${enumCol1["name"]}">${enumCol1["value"]}</el-radio>
                          </#list>
                        </#if>
                      </#if>
                    </#list>
                  </el-radio-group>
                </#if>
              <#elseif col["updateType"] == 'number'>
                <el-input-number v-model="formData.${col["propertyName"]}" :min="1" :max="100" ></el-input-number>
              <#elseif col["updateType"] == 'date'>
                <el-date-picker
                        v-model="formData.${col["propertyName"]}"
                        format="yyyy 年 MM 月 dd 日"
                        value-format="yyyy-MM-dd"
                        type="date"
                        placeholder="请选择日期">
                </el-date-picker>
              <#elseif col["updateType"] == 'textArea'>
                <el-input type="textarea"  v-model="formData.${col["propertyName"]}"></el-input>
              </#if>
            </el-form-item>
          </#list>
        </#if>
      </el-form>
      <div slot="footer">
        <el-button @click="reset(formData)">重置</el-button>
        <el-button @click="closeDia(formData)">取消</el-button>
        <el-button type="primary" @click="saveData">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="imgVisible" width="400px">
      <img width="100%" :src="imgDiaUrl" alt=""/>
    </el-dialog>
  </div>
</template>

<script>
  import {util} from '../utils/index';

  export default {
    mixins: [util],
    data() {
      return {
        // =========    start     ===========
        imgVisible: false, // 图片展示
        imgDiaUrl: "",
        moduleName: "/${unClassName}",
        header: '',
        delVisible: false,// 删除对话框
        openDia: false, // 添加或修改对话框
        params: { // 查询参数 可自行添加参数 但除search外的都不可变
          current: 1,
          pageSize: 5,
          total: 0,
          <#if vueSearchColList??>
          <#list vueSearchColList as col>
          ${col["propertyName"]}: undefined,
          </#list>
          </#if>
        },
        tableData: [], // 表格数据
        diaTitle: '添加', // 添加或修改框标题
        multipleSelection: [], // 批量删除数组
        // =========    end     ===========
        formData: {
          <#if showColList??>
          <#list showColList as col>
          <#if col["propertyName"] != 'createTime' && col["propertyName"] != 'updateTime'>
          ${col["propertyName"]}: undefined,
          </#if>
          </#list>
          </#if>
        },
        rules: {
          <#if vueModifyColList??>
          <#list vueModifyColList as col>
          <#if col["propertyName"] != 'createTime' && col["propertyName"] != 'updateTime'>
          ${col["propertyName"]}: [{ required: true, message: '${col["propertyChinaName"]}为必须项~', trigger: 'blur' }],
          </#if>
          </#list>
          </#if>
        },
        <#if selectEntities??>
        <#list selectEntities as col>
        ${col["className"]}Data:[],
        </#list>
        </#if>
      };
    },

    mounted() {
      this.getTableData();
      <#if selectEntities??>
      <#list selectEntities as col>
      this.get${col["className"]}Data()
      </#list>
      </#if>
    },
    methods: {

      getTableData(val) {
        if (val) this.params.current = 1;
        this.$getTableData(this.moduleName, this.params, this, "/page")
      },
      saveData() {
        this.$saveData(this.moduleName, this.formData, this, 'elForm');
      },
      deleteByIds() {
        this.$deleteByIds(this.moduleName, this.multipleSelection, this);
      },
      deleteById(id) {
        this.$deleteById(this.moduleName, id, this);
      },
      clearParam() {
        this.params.userName = undefined;
        this.getTableData();
      },
      <#if selectEntities??>
      <#list selectEntities as col>
      get${col["className"]}Data(){
        let that = this
        this.$baseGetRequest(this.moduleName + "/get${col["className"]}Data").then(res => {
           that.${col["className"]}sData = res
        })
      },
      </#list>
      </#if>
    }
  };
</script>
